<?php
include 'config.php';
session_start();
$bagian	   = $_GET['bagian'];
$subbagian = $_GET['subbagian'];

if( validUser($subbagian) == TRUE ){
	if( isset($_GET['q']) ){ // EDIT
		$q  = decodeQuery($_GET['q']);
		$editHandler->createForm($bagian, $subbagian, $q);
	}
	else{
		// updating..
		$tableConfig = $insertConfig[$subbagian];
		$q = 'UPDATE '.$tableConfig['table'].' SET ';
		// updating

		$i = 0;
	    foreach($tableConfig['field'] as $f){
			if( !in_array($f, $tableConfig['primary']) ){
				if( $i == 0 )
					$q .= "`".$f."`='".$_GET[$f]."'";
				else
				    $q .= ", `".$f."`='".$_GET[$f]."'";

				$i++;
			}
		}

		//where
		$q .= ' WHERE ';
		$i=0;
		foreach($tableConfig['primary'] as $u){
			if($i==0)
				$q .= "`".$u."`='".$_GET[$u]."'";
			else
				$q .= " AND `".$u."`='".$_GET[$u]."'";

			$i++;
		}
		
		$db->query($q); 
		echo "
		<script>
			opener.window.location.href='index.php?bagian=".$bagian."&subbagian=".$subbagian."';
			window.close();
		</script>";
	}
}
?>